package com.wangxiaoxuan.seckill.service.impl;

import com.wangxiaoxuan.seckill.Util.CookieUtil;
import com.wangxiaoxuan.seckill.Util.Md5Util;
import com.wangxiaoxuan.seckill.Util.UUIDUtil;
import com.wangxiaoxuan.seckill.pojo.User;
import com.wangxiaoxuan.seckill.mapper.UserMapper;
import com.wangxiaoxuan.seckill.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxiaoxuan.seckill.service.ex.BaseException;
import com.wangxiaoxuan.seckill.vo.LoginVo;
import com.wangxiaoxuan.seckill.vo.ResBeanEnum;
import com.wangxiaoxuan.seckill.vo.RespBean;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangxiaoxuan
 * @since 2022-03-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param loginVo
     * @param request
     * @param response
     * @return user
     */
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        //拿到浏览器传过来的数据，登录参数以LoginVo对象的形式传递过来
        Long mobile = loginVo.getMobile();
        String inpassword = loginVo.getPassword();
        String password = Md5Util.inputPassToFromPass(inpassword);
        //判断传递过来的数据是否为null
        if (mobile==null||password==null){
            //抛出异常
           throw new BaseException(ResBeanEnum.LOGIN_ERROR);
        }
        User user = userMapper.selectById(loginVo.getMobile());
        Long id = user.getId();
        String pwd = user.getPassword();
        String salt=user.getSalt();
        if (user == null){
            throw new BaseException(ResBeanEnum.LOGIN_ERROR);
        }
        if (!(mobile.equals(id)&Md5Util.fromPassToDBPass(password,salt).equals(pwd))){
            throw new BaseException(ResBeanEnum.LOGIN_ERROR);
        }
        //生成cookie
        String ticket = UUIDUtil.uuid();
        //request.getSession().setAttribute(ticket,user);
        //将用户信息存入redis,String类型
        redisTemplate.opsForValue().set("user:"+ticket,user);
        //使用cookie工具类，生成缓存
        CookieUtil.setCookie(request,response,"userTicket",ticket);//不设置生效时间默认浏览器关闭即失效,也不编码
        return RespBean.success();

    }

    /**
     * 通过cookie得到user对象
     * @param userTicket
     * @param request
     * @param response
     * @return
     */
    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)){
            return null;
        }
       User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        if (user!=null){
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }

}
